$.get('http://localhost:3000/user', data => {
    data.filter(item => {
        let html = `
    <tr>
    <td>${item.id}</td>
    <td>${item.biaoti}</td>
    <td>${item.zhaiyao}</td>
    <td>${item.neirong}</td>
    <td>${item.fenlei}</td>
    <td>${item.zuozhe}</td>
    <td>${item.createdAt}</td>
    <td>
        <input type="button" value="编辑" onclick="bian(${item.id})">
        <input type="button" value="删除" onclick="shan(${item.id})">
    </td>
</tr>
    `
        $('#tbdate').append(html)
    })
})


function cha() {
    let word = $('#word').val()
    console.log(word);
    $('#tbdate tr:gt(0)').remove()
    $.get(`http://localhost:3000/user/${word}`, data => {
        console.log(data);
        if (data.code == 1000) {
            data.data.filter(item => {
                let html = `
    <tr>
    <td>${item.id}</td>
    <td>${item.biaoti}</td>
    <td>${item.zhaiyao}</td>
    <td>${item.neirong}</td>
    <td>${item.fenlei}</td>
    <td>${item.zuozhe}</td>
    <td>${item.createdAt}</td>
    <td>
        <input type="button" value="编辑" onclick="bian(${item.id})">
        <input type="button" value="删除" onclick="shan(${item.id})">
    </td>
</tr>
    `
                $('#tbdate').append(html)
            })
        } else {
            alert('没有查找到输入的关键字内容，已为您加载全部数据！！！')
            window.location.href = 'index.html'
        }
    })
}
function add() {
    window.location.href = 'add.html'
}

let id = window.location.search.substring(1)
if (id) {
    $.get('http://localhost:3000/user', data => {
        console.log(data);
        data.filter(item => {
            if (item.id == id) {
                $('#biaoti').val(item.biaoti)
                $('#zhaiyao').val(item.zhaiyao)
                $('#neirong').val(item.neirong)
                $('#fenlei').val(item.fenlei)
                $('#zuozhe').val(item.zuozhe)
            }
        })
    })
}

function baocun() {
    if (id) {
        let biaoti = $('#biaoti').val()
        let zhaiyao = $('#zhaiyao').val()
        let neirong = $('#neirong').val()
        let fenlei = $('#fenlei').val()
        let zuozhe = $('#zuozhe').val()
        let obj = {
            id,
            biaoti,
            zhaiyao,
            neirong,
            fenlei,
            zuozhe,
        }
        $.ajax({
            url: 'http://localhost:3000/user',
            type: 'put',
            data: obj,
            success: function (res) {
                window.location.href = 'index.html'
            }
        })
    } else {
        let biaoti = $('#biaoti').val()
        let zhaiyao = $('#zhaiyao').val()
        let neirong = $('#neirong').val()
        let fenlei = $('#fenlei').val()
        let zuozhe = $('#zuozhe').val()
        let obj = {
            biaoti,
            zhaiyao,
            neirong,
            fenlei,
            zuozhe
        }
        $.post('http://localhost:3000/user', obj, res => {
            if (res == 1000) {
                window.location.href = 'index.html'
            } else {
                alert('标题和摘要不能为空！！！')
            }
        })
    }
}
function quxiao() {
    window.location.href = 'index.html'
}

function bian(id) {
    window.location.href = `add.html?${id}`
}

function shan(id) {
    let msg = confirm('确认删除吗？')
    if (msg) {
        $.ajax({
            url: `http://localhost:3000/user/${id}`,
            type: 'delete',
            success: function (res) {
                window.location.href = 'index.html'
            }
        })
    }
}